<template>
	<view class="page">
		<view class="logo">
			<image src="../../static/logo.png" mode=""></image>
		</view>
		<!-- 填写区 -->
		<view class="input-info">
			<view class="info">
				<input type="tel" maxlength="11" v-model="form.phone" placeholder="手机号">
				<view class="more"></view>
			</view>
			<view class="info" :style="isLoginWay?'':'display: none'">
				<input type="tel" v-model="form.code" maxlength="6" placeholder="请输入验证码">
				<view class="more">
					<text class="mo" @click="getCode" :style="isSend?'display:block':'display: none'">获取验证码</text>
					<text class="mo" :style="isSend?'display: none':'display:block'"
						v-model="second">{{second}}秒后重试</text>
				</view>
			</view>
			<view class="info" :style="isLoginWay?'display: none':''">
				<input :password='!isPassword' v-model="form.password" maxlength="26" placeholder="请输入密码">
				<view class="more">
					<text class="iconfont" :class="isPassword?'icon-eye-on':'icon-eye-off'"
						@click="isPassword = !isPassword"></text>
					<text class="mo">忘记密码</text>
				</view>
			</view>
		</view>
		<!-- 按钮 -->
		<view class="btn-info">
			<view class="btn" :style="isLogin?'opacity:1':'opacity:0.4'" @click="isLogin?onLogin():''">
				<text>登录</text>
			</view>
		</view>
		<!-- 操作 -->
		<view class="operation">
			<text @click="onLoginCut">{{isLoginWay?'手机号密码登录':'短信验证码登录'}}</text>
			<text @click="onRegister">新用户注册</text>
		</view>
		<!-- 其他方式登录 -->
		<view class="other-ways">
			<text>其他登录方式</text>
		</view>
		<!-- 登录方式 -->
		<view class="login-way">
			<view class="way">
				<image src="/static/wx_ico.png" mode=""></image>
				<text>微信登录</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		name: "login",
		data() {
			return {
				isLogin: false,
				isLoginWay: false,
				isPassword: false,
				isSend: true,
				second: 59,
				// 表单
				form: {
					phone: '',
					code: '',
					password: '',
				},
			};
		},
		computed: {
			...mapState(["login"])
		},
		methods: {
			onRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},
			/**
			 * 登录切换
			 */
			onLoginCut() {
				this.isLoginWay = !this.isLoginWay;
				// 验证码
				if (this.isLoginWay) {
					this.isSend = true;
					this.second = 60;
					this.isLogin = this.form.code && this.form.phone ? true : false;
				}
				// 账号密码
				if (!this.isLoginWay) {
					this.isLogin = this.form.password && this.form.phone ? true : false;
				}
			},
			getCode() {
				if (this.form.phone == '' || this.form.phone == '手机号') {
					alert("手机号不能为空！");
					return;
				}

				this.isSend = false;
				this.second = 60;
				console.log(this.phone);
				uni.request({
					data: {
						phone: this.form.phone
					},
					method: 'POST',
					header: {
						"content-type": 'application/x-www-form-urlencoded'
					},
					url: "http://localhost:8089/sendSms",
					success: (res) => {
						if (res.data.success) {
							
							uni.showToast({
								title: '发送成功',
								duration: 3000
							});
						} else {
							uni.showToast({
								title: res.data.message,
								icon: "none",
								duration: 3000
							});
						}
					}
				});
				var time = 60;
				var _this = this;
				var intervalId = setInterval(function() {
					time = time - 1;
					_this.second = time;
					if (_this.second <= 0) {
						_this.isSend = true
						clearInterval(intervalId);
					}
				}, 1000)

			},
			/**
			 * 登录点击
			 */
			onLogin() {
				setTimeout(()=>{
					
				},1000)
				this.$queue.showLoading("正在登录中");
				uni.request({
					data: {
						phone: this.form.phone,
						password: this.form.password
					},
					method: 'POST',
					header: {
						"content-type": 'application/x-www-form-urlencoded'
					},
					url: "http://localhost:8089/cgoAdmin/login",
					success: (res) => {
						
//						alert(this.$queue.getJson("id"));  获取token方法
						
						if (res.data.success) {
							this.$queue.showLoading("登录成功");
							this.$queue.setData("token", res.data.result.token);
							this.$queue.setData("id",  res.data.result.id);
							this.$queue.setData("phone",  res.data.result.phone);
							this.$queue.setData("username",  res.data.result.username);
							uni.switchTab({
								url: "/pages/home/home"
							})
						} else {
							if (!this.isLoginWay) {
								this.$queue.showToast("用户名或密码错误");
								
							} else {
								this.$queue.showToast("用户名或验证码错误！");
							
							}
						}
					}
				});

				if (this.isLoginWay) {

					uni.request({
						data: {
							phone: this.form.phone,
							code: this.form.mobile
						},
						method: 'POST',
						header: {
							"content-type": 'application/x-www-form-urlencoded'
						},
						url: "http://localhost:8089/cgoAdmin/Smslogin",
						success: (res) => {
							if (res.data.success) {
								this.$queue.showToast("登录成功");
								this.$queue.setData("token", res.data.result.token);
								this.$queue.setData("id",  res.data.result.id);
								this.$queue.setData("phone",  res.data.result.phone);
								this.$queue.setData("username",  res.data.result.username);
								uni.switchTab({
									url: "/pages/home/home"
								})

							} else {
								this.$queue.showToast("用户名或密码错误");
								
							}
						}
					});
				}
			}
		},
		watch: {
			form: {
				handler(newValue, oldValue) {
					// 验证码
					if (this.isLoginWay) {
						this.isLogin = newValue.code && newValue.phone ? true : false;
					}
					// 账号密码
					if (!this.isLoginWay) {
						this.isLogin = newValue.password && newValue.phone ? true : false;
					}
				},
				deep: true
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'login.scss';
</style>
